package com.zhugang.week13.stack;

import java.util.Stack;

/**
 * @program algorithms
 * @description: StackToQueue
 * @author: chanzhugang
 * @create: 2022/11/01 19:44
 */
public class StackToQueue {

    /**
     * 栈实现队列：用两个栈实现，互相倒腾
     */
    Stack<Integer> stack = new Stack<>();
    Stack<Integer> tempStack = new Stack<>();

    /**
     * 入队直接入
     *
     * @param data
     */
    public void enqueue(int data) {
        stack.push(data);
    }

    /**
     * 出队倒腾两个栈，画个图就好理解
     *
     * @return
     */
    public int dequeue() {
        if (stack.isEmpty()) {
            return -1;
        }
        while (!stack.isEmpty()) {
            tempStack.push(stack.pop());
        }
        Integer pop = tempStack.pop();
        while (!tempStack.isEmpty()) {
            // 临时栈的数据最终都要倒腾到原有栈
            stack.push(tempStack.pop());
        }
        return pop;
    }
}